package design;

import design.forkjoin.RecuresiceAction1;
import design.masterWorker.Result;
import design.masterWorker.SalaryRunner;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

public class ForkJoinTest4 {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+ "run");
            }
        });
        ForkJoinTask<?> submit = pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+ "run");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 获取结果为null，依然具有阻塞特性
        try {
            System.out.println("result01:" + submit.get());
            System.out.println("result02:" + submit.join());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        // 提交Runnable线程任务，获取结果
        Result result = new Result();
        ForkJoinTask<Result> submit1 =
                pool.submit(new SalaryRunner(result,100,1000),result);
        System.out.println("reslult03" + submit1.join().getValue());
        System.out.println("reslult04" + submit1.join().getValue());
        // execute方法无返回值，但是任然可以使用get和join获取结果
        RecuresiceAction1 ta = new RecuresiceAction1(1,5);
        pool.execute(ta);
        try {
            System.out.println("reslult05" + ta.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
